﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.IO;
using QUANLIBANHANG.BUS;
using QUANLIBANHANG.ENTITY;
using System.Security.Cryptography;
namespace QUANLIBANHANG.GUI
{
    public partial class formInfoTaiKhoan : DevExpress.XtraEditors.XtraForm
    {
        NhanvienBUS nvBUS;
        DataTable tableNV;
        NhanvienEntity nvEntity = new NhanvienEntity();
       // QUANLIBANHANGGUI gu = new QUANLIBANHANGGUI();
        public formInfoTaiKhoan()
        {
            InitializeComponent();
            nvBUS = new NhanvienBUS();
           
            
        }
        private void formInfoTaiKhoan_Load(object sender, EventArgs e)
        {

            tableNV = NhanvienBUS.getTableNV("");
            panelMatKhau.Visible = false;

            DataRow[] row = tableNV.Select("MANV='" + lblTenTaiKhoan.Text + "'");
            lblNgayVL.Text = row[0]["NGAYVL"].ToString();
            lblHoten.Text = row[0]["HOTEN"].ToString();
            txtHoten.Text = lblHoten.Text;
            lblLoaiTK.Text = row[0]["CHUCVU"].ToString();
            dteNgaysinhTK.Text = row[0]["NGAYSINH"].ToString();
            txtSDT.Text = row[0]["SDT"].ToString();
            txtEmail.Text = row[0]["EMAIL"].ToString();
            txtDiaChi.Text = row[0]["DIACHI"].ToString();
            picTaiKhoan.Image = byteArrayToImage((byte[])row[0]["AVATAR"]);
        }
        public void receiveMess(string mess)
        {
            lblTenTaiKhoan.Text = mess;
        }

        // Chuyển image sang dạng byte[]
        public Image byteArrayToImage(byte[] byteArrayIn)
        {
            MemoryStream ms = new MemoryStream(byteArrayIn);
            Image returnImage = Image.FromStream(ms);
            return returnImage;

        }
        //Chuyển dạng byte[] sang image
        public byte[] imageToByteArray(System.Drawing.Image imageIn)
        {
            MemoryStream ms = new MemoryStream();
            imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
            return ms.ToArray();
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if(panelMatKhau.Visible == false)
            {
                panelMatKhau.Visible = true;
            }
            else
            {
                panelMatKhau.Visible = false;
            }
            
        }
        
        private void getData()
        {
            
            nvEntity.Hoten = txtHoten.Text;
            nvEntity.Manv = lblTenTaiKhoan.Text;
            nvEntity.Ngaysinh = dteNgaysinhTK.DateTime.ToString("MM-dd-yyyy");
           
            nvEntity.Sdt = txtSDT.Text;
            DateTime date = Convert.ToDateTime(lblNgayVL.Text);
            nvEntity.Ngayvl = date.ToString("MM-dd-yyyy");
            nvEntity.Chucvu = lblLoaiTK.Text;
            nvEntity.Email = txtEmail.Text;
            nvEntity.Diachi = txtDiaChi.Text;
            nvEntity.Avatar = imageToByteArray(picTaiKhoan.Image);
        }
        // Lưu lại trang thái cửa tài khoan
        private void btnLuu_Click(object sender, EventArgs e)
        {
            DataRow[] row = tableNV.Select("MANV='" + lblTenTaiKhoan.Text + "'");
            if (panelMatKhau.Visible != false)
            {
                if (txtPassHienTai.Text == "" || txtPassMoi.Text == "" || txtPassMoiXn.Text == "")
                {
                    MessageBox.Show("Thiếu thông tin đễ thay đỗi mật khẩu", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                else
                {
                    if (toMD5(txtPassHienTai.Text) != row[0]["PASS"].ToString())
                    {
                        MessageBox.Show("Mật khẩu hiện tại bị sai, vui lòng nhập lại!", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    else if (txtPassMoi.Text != txtPassMoiXn.Text)
                    {
                        MessageBox.Show("Mật khẩu mới và mật khẩu xách nhận không giống nhau", "Lỗi");
                        return;
                    }
                    else
                    {
                        getData();
                        nvEntity.Pass=toMD5(txtPassMoi.Text);
                        NhanvienBUS.updateNV(nvEntity);
                        NhanvienBUS.updatePass(nvEntity);

                    }

                }
            }
            else
            {
                getData();
                nvEntity.Pass = row[0]["PASS"].ToString();
               NhanvienBUS.updateNV(nvEntity);
            }
            this.Close();
            
           // gu.Refresh();
        }
        // Thuật toán mã hoa MD5
        public static string toMD5(string pass)
        {
            MD5CryptoServiceProvider myMD5 = new MD5CryptoServiceProvider();
            byte[] myPass = System.Text.Encoding.UTF8.GetBytes(pass);
            myPass = myMD5.ComputeHash(myPass); StringBuilder s = new StringBuilder();
            foreach (byte p in myPass)
            {
                s.Append(p.ToString("x").ToLower());
            }
            return s.ToString();
        }
        
        private void picTaiKhoan_Click(object sender, EventArgs e)
        {
            OpenFileDialog openpic = new OpenFileDialog();
            openpic.RestoreDirectory = true;
            openpic.FilterIndex=2;
            openpic.Filter = "Jpg(*.jpg)|*.jpg";
            if (openpic.ShowDialog() == DialogResult.OK)
            {
                picTaiKhoan.ImageLocation = openpic.FileName;
            }

        }
    }
}